package net.zjitc.service.impl;

import net.zjitc.mapper.AdminMapper;
import net.zjitc.pojo.Admin;
import net.zjitc.service.AdminService;
import net.zjitc.utils.MD5WithSaltUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public Admin findAdmin(String username,String password) {
        Admin admin = adminMapper.findAdmin(username);
        if(admin==null){
            return null;
        }
        password = MD5WithSaltUtil.hashPasswordSalt(password, admin.getSalt());
        if(password.equals(admin.getPassword())){
            return admin;
        }
        return null;
    }

    @Override
    public Admin admin() {
        Admin admin = adminMapper.admin();
        return admin;
    }

    @Override
    public void changeAvatar(String avatar) {
        adminMapper.changeAvatar(avatar);
    }

    @Override
    public void changePassword(String password) {
        Admin admin = adminMapper.findAdmin("admin");
        password = MD5WithSaltUtil.hashPasswordSalt(password, admin.getSalt());
        adminMapper.changePassword(password);
    }

}
